package jafou.client.ui.objects;

import jafou.client.remote.JafouService;
import jafou.client.remote.JafouServiceAsync;
import jafou.client.ui.ForumWidget;

import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;

public class MiscPanel extends HorizontalPanel {
	
	private ForumWidget ui;
	private final JafouServiceAsync handler = GWT.create(JafouService.class);
	
	public MiscPanel(ForumWidget ui) {
		this.ui = ui;
		showForumControls(null);
	}
	
	private class NewForumClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			ui.renderNewForumPanel();
		}
	}
	
	private class DelForumClickHandler implements ClickHandler {
		private String forumId;
		public DelForumClickHandler(String forumId) {
			this.forumId = forumId;
		}

		@Override
		public void onClick(ClickEvent event) {
			if(Window.prompt("delete forum?", "cancel") != null ){
				handler.delForum(forumId, delForumCallback);
			}
		}
		
	}
	
	private class CreateTopicClickHandler implements ClickHandler {
		private String forumId;
		public CreateTopicClickHandler(String forumId) {
			this.forumId = forumId;
		}

		@Override
		public void onClick(ClickEvent event) {
			ui.renderNewTopicPanel(forumId);
		}
	}
	
	private class PostClickHandler implements ClickHandler {
		private String topicId;
		public PostClickHandler(String topicId) {
			this.topicId = topicId;
		}
		@Override
		public void onClick(ClickEvent event) {
			ui.renderNewPostPanel(topicId);
		}
	}
	
	private class DelTopicClickHandler implements ClickHandler {
		private String topicId;
		public DelTopicClickHandler(String topicId) {
			this.topicId = topicId;
		}
		@Override
		public void onClick(ClickEvent event) {
			if(Window.prompt("delete topic?", "cancel").equalsIgnoreCase("ok") ){
				handler.delTopic(topicId, delTopicCallback);
			}
		}
	}
	
	private AsyncCallback<String> delForumCallback = new AsyncCallback<String>() {
		@Override
		public void onFailure(Throwable caught) {
		}

		@Override
		public void onSuccess(String result) {
			ui.handleForumDeletion();
		}
	};

	private AsyncCallback<String> delTopicCallback = new AsyncCallback<String>() {
		@Override
		public void onFailure(Throwable caught) {
		}

		@Override
		public void onSuccess(String result) {
			ui.handleTopicDeletion(result);
		}
	};
	
	private class UserIdCallBack implements AsyncCallback<String> {
		private String topicId;
		public UserIdCallBack(String topicId) {
			this.topicId = topicId;
		}
		@Override
		public void onFailure(Throwable caught) {
			continueShowTopicControls(topicId, null);
		}
		@Override
		public void onSuccess(String result) {
			continueShowTopicControls(topicId, result);
		}
	}
	
	private void clearContents() {
		while(getWidgetCount() > 0) {
			remove(0);
		}
	}
	
	public void showForumControls(String forumId) {
		clearContents();
		String loggedIn = ui.getUserAttribute("logged_in");
		String isAdmin = ui.getUserAttribute("is_admin");
		if(loggedIn != null && loggedIn.equals("true")){
			if(forumId != null) {
				add(new Button("start topic", new CreateTopicClickHandler(forumId)));
			}
			if(isAdmin != null && isAdmin.equals("true")) {
				add(new Button("new forum", new NewForumClickHandler()));
				if(forumId != null) {
					add(new Button("delete forum", new DelForumClickHandler(forumId)));
				}
			}
		} else {
			add(new Label("please log in to post"));
		}
	}
	
	public void showTopicControls(String topicId) {
		String loggedIn = ui.getUserAttribute("logged_in");
		boolean logged = (loggedIn != null && "true".equals(loggedIn));
		if(!logged) {
			add(new Label("please log in to post"));
			return;
		}
		handler.getUserIdForTopic(topicId, new UserIdCallBack(topicId));
	}
	
	public void continueShowTopicControls(String topicId, String userId) {
		clearContents();
		String loggedIn = ui.getUserAttribute("logged_in");
		String isAdmin = ui.getUserAttribute("is_admin");
		String currentUserId = ui.getUserAttribute("id");
		boolean logged = (loggedIn != null && "true".equals(loggedIn));
		boolean admin = (isAdmin != null && "true".equals(isAdmin));
		if (logged && topicId != null) {
			add(new Button("post message", new PostClickHandler(topicId)));
		}
		if (admin || (currentUserId != null
				&& userId != null
				&& currentUserId.equals(userId))) {
			add(new Button("delete topic", new DelTopicClickHandler(topicId)));
		}
	}
	
}
